package com.example.litemall.admin.api.service.impl;

import com.example.litemall.admin.api.domain.LitemallBrand;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.litemall.admin.api.mapper.LitemallBrandMapper;
import com.example.litemall.admin.api.service.LitemallBrandService;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallBrandServiceImpl implements LitemallBrandService{

    @Resource
    private LitemallBrandMapper litemallBrandMapper;


    @Override
    public void addBrand(LitemallBrand brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        litemallBrandMapper.insertSelective(brand);

    }

    @Override
    public List<LitemallBrand> getBrandList(LitemallBrand brand, Integer page, Integer limit) {
        //分页
        PageHelper.startPage(page,limit);
        //条件搜索
        Example example = new Example(LitemallBrand.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(brand.getId())){
            criteria.andEqualTo("id",brand.getId());
        }
        if(!StringUtils.isEmpty(brand.getName())){
            criteria.andLike("name","%"+brand.getName()+"%");
        }
        criteria.andEqualTo("deleted",false);

        List<LitemallBrand> litemallBrands = litemallBrandMapper.selectByExample(example);
        return litemallBrands;
    }

    @Override
    public void updateBrand(LitemallBrand brand) {
            brand.setUpdateTime(LocalDateTime.now());
            litemallBrandMapper.updateByPrimaryKeySelective(brand);
    }
}
